package com.shenzhen.horselive.ui.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flqy.baselibrary.utils.L;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.SendRecviRedAdapter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.BaseFragment;
import com.shenzhen.horselive.beans.HandOutBean;
import com.shenzhen.horselive.beans.HaveReceviedRed;
import com.shenzhen.horselive.beans.RedPackageBean;
import com.shenzhen.horselive.beans.RedPageListBean;
import com.shenzhen.horselive.common.utils.handler.IFilterHandler;
import com.shenzhen.horselive.databinding.FragmentSendRedDetailsBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.presenter.ViewObserver;
import com.shenzhen.horselive.ui.activity.HongBaoDetailsActivity;
import com.shenzhen.horselive.ui.activity.RedPackageDetailsActivity;

import java.util.ArrayList;
import java.util.List;

public class SendRedDetailsFragment extends BaseFragment {
    private static final String ARG_PARAM1 = "param1";

    // TODO: Rename and change types of parameters
    private String mParam1;

    public SendRedDetailsFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @return A new instance of fragment SendRedDetailsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SendRedDetailsFragment newInstance(String param1) {
        SendRedDetailsFragment fragment = new SendRedDetailsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    FragmentSendRedDetailsBinding   binding;

    @Override
    protected View getContentView(LayoutInflater inflater, ViewGroup viewGroup) {
        binding  =FragmentSendRedDetailsBinding.inflate(getLayoutInflater(),viewGroup,false);
        initViews();
        initTongji();
        initData(0);
        return binding.getRoot();
    }

    //统计数量
    private void initTongji() {
        if ("1".equals(mParam1))
        {
            //收到
            ApiFactory.getApiService().haveReceivedRed(User.get().getMyInfo().getToken())
                    .compose(SchedulersCompat.applyIoSchedulers())
                    .safeSubscribe(new ViewObserver<HaveReceviedRed>(this) {
                        @Override
                        protected void onNextInActive(HaveReceviedRed haveReceviedRed) {
                            if (haveReceviedRed.isSuccess())
                            {
                                binding.tvMoney1.setText(formatFuckingNumber(haveReceviedRed.getData().getReceiveAmount())+"元");
                                binding.tvMony2.setText(formatFuckingNumber(haveReceviedRed.getData().getWalletAmount())+"元");

                            }
                        }
                    });
        }
        else
        {

            ApiFactory.getApiService().handOutRed(User.get().getMyInfo().getToken())
                    .compose(SchedulersCompat.applyIoSchedulers())
                    .safeSubscribe(new ViewObserver<HandOutBean>(this) {
                        @Override
                        protected void onNextInActive(HandOutBean handOutBean) {
                            if (handOutBean.isSuccess())
                            {
                                binding.tvMoney1.setText(handOutBean.getData().getTotalAmount()+"元");
                                binding.tvMony2.setText(handOutBean.getData().getAffectPeople()+"人");
                            }
                        }
                    });

        }
    }

    /**
     * 页码暂时传1
     * @param page
     */
    private void initData(int page) {
        if ("1".equals(mParam1))
        {
            //收到的红包
            ApiFactory.getApiService().getRedPackageList(User.get().getMyInfo().getToken(),page+"")
                    .compose(SchedulersCompat.applyIoSchedulers())
                    .safeSubscribe(new LoadingViewObserver<RedPageListBean>(this) {
                        @Override
                        protected void onNextInActive(RedPageListBean redPageListBean) {
                            if (binding.smartRefresh.isLoading())
                            {
                                binding.smartRefresh.finishLoadMore();
                            }
                            if (binding.smartRefresh.isRefreshing())
                            {
                                binding.smartRefresh.finishRefresh();
                            }
                            if (redPageListBean.isSuccess())
                            {
                                if (page==0)
                                {
                                adapter.setNewData(redPageListBean.getData());
                                }
                                else
                                {
                                    if (redPageListBean.getData()==null||redPageListBean.getData().size()==0)
                                    {
                                        binding.smartRefresh.finishLoadMoreWithNoMoreData();
                                    }
                                    else
                                    {
                                        adapter.addData(redPageListBean.getData());
                                    }
                                }
                            }
                            else
                            {
                                showToast(redPageListBean.getErrMessage());
                            }
                        }
                    });
        }else
        {
            //发出去的红包
            ApiFactory.getApiService().sendRedPackageList(User.get().getMyInfo().getToken(),page+"")
                    .compose(SchedulersCompat.applyIoSchedulers())
                    .safeSubscribe(new ViewObserver<RedPageListBean>(this) {
                        @Override
                        protected void onNextInActive(RedPageListBean redPageListBean) {
                            if (binding.smartRefresh.isLoading())
                            {
                                binding.smartRefresh.finishLoadMore();
                            }
                            if (binding.smartRefresh.isRefreshing())
                            {
                                binding.smartRefresh.finishRefresh();
                            }
                            if (redPageListBean.isSuccess())
                            {
                                if (page==0)
                                {
                                    adapter.setNewData(redPageListBean.getData());
                                }
                                else
                                {
                                    if (redPageListBean.getData()==null||redPageListBean.getData().size()==0)
                                    {
                                        binding.smartRefresh.finishLoadMoreWithNoMoreData();
                                    }
                                    else
                                    {
                                        adapter.addData(redPageListBean.getData());
                                    }
                                }
                            }
                            else
                            {
                                showToast(redPageListBean.getErrMessage());
                            }
                        }
                    });
        }

    }

    
    SendRecviRedAdapter  adapter;
    private  int  nowPage = 0;
    private void initViews() {
        if (mParam1.equals("1"))
        {
            binding.tvText.setText("收到总金额(元)");
            binding.tvText1.setText("钱包总金额（元）");
        }
        else
        { binding.tvText.setText("红包总金额(元)");
            binding.tvText1.setText("累计影响(人)");

        }

        binding.recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new SendRecviRedAdapter(R.layout.item_fragment_send_red);
        binding.recyclerview.setAdapter(adapter);
        adapter.setEmptyView(R.layout.no_data_layout);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
//                Intent  intent = new Intent(getContext(), RedPackageDetailsActivity.class);
//                intent.putExtra("id",((RedPackageBean)adapter.getItem(position)).getId()+"");
//                startActivity(intent);
                //跳转到红包详情页面
                HongBaoDetailsActivity.start(getContext(),mParam1,((RedPackageBean)adapter.getItem(position)).getId()+"");
            }
        });
        binding.smartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                initData(++nowPage);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                  nowPage=0;
                  initData(nowPage);
            }
        });


    }

    @Override
    public void initImmersionBar() {

    }
}